package com.ltt.service.impl;

import com.ltt.dao.pojo.Chat;
import com.ltt.dao.pojo.Group;
import com.ltt.dao.pojo.LastChat;
import com.ltt.dao.pojo.User;
import com.ltt.mapper.ChatMapper;
import com.ltt.mapper.GroupMapper;
import com.ltt.mapper.UserInfoMapper;
import com.ltt.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ‘李天泰’
 * @version 1.0
 * @description: TODO
 * @date 2023/5/10 14:56
 */
@Service
public class GroupServiceImpl implements GroupService {
    @Autowired
    private GroupMapper groupMapper;
    @Autowired
    private ChatMapper chatMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;
    public List<Group> findAllGroupById(String openId){
        List<String> groupIds = groupMapper.findAllGroupById(openId);
        List<Group> groups = new ArrayList();
        for(String id : groupIds) {
            Group group = groupMapper.findGroupById(id);
            int res = groupMapper.findIsA(id, openId);
            group.setIsRead(res);
            group.setLastChat(this.getLastChat(id));
            groups.add(group);
        }

        return groups;
    };
    @Override
    public boolean exitGroup(String groupId, String openId) {
        int res = groupMapper.exitGroup(groupId, openId);
        return res > 0;
    }

    @Override
    public LastChat getLastChat(String groupId) {
        Chat chat = chatMapper.getLastChat(groupId);
        if(chat == null) {
            return null;
        }
        User user = userInfoMapper.findUserById(chat.getInitiator());
        LastChat lastChat = new LastChat();
        lastChat.setLastContent(user.getNickName() + ':' + chat.getContent());
        lastChat.setSendTime(chat.getSendTime());
        return lastChat;
    }

    @Override
    public Group findGroupById(String groupId, String openId) {
        Group group = groupMapper.findGroupById(groupId);
        int res = groupMapper.findIsA(groupId, openId);
        group.setIsRead(res);
        return group;
    }

    @Override
    public boolean updateGroupItem(Group group) {
        int res = groupMapper.updateGroupItem(group);
        return res > 0;
    }

    @Override
    public List<Group> findAllGroup(String openId){
        List<Group> groups = groupMapper.findAllGroup();
        for(Group group : groups) {
            LastChat chat = this.getLastChat("" + group.getId());
            int res = groupMapper.groupIdFindAdd(group.getId() + "", openId);
            if(res > 0) {
                group.setIsRead(1);
            } else {
                group.setIsRead(0);
            }
            if(chat != null){
                group.setLastChat(chat);
            }

        }
        return groups;
    }

    @Override
    public boolean insert(Group group, String openId) {
        int res = groupMapper.insertGroup(group);
        int max = groupMapper.findMax();
        int ress = groupMapper.addGroup(max, openId, 1);
        return res > 0;
    }

    @Override
    public boolean addGroup(int groupId, String openId, int isA){
        Group group = groupMapper.findGroupById(groupId + "");
        System.out.println(group);
        System.out.println(groupId + "," + openId + "," + isA);
        if(group.getNowNum() < group.getNum()){
            group.setNowNum(group.getNowNum() + 1);
            int resss = groupMapper.updateGroupItem(group);
            System.out.println(resss);
            int ress = groupMapper.findGroup(groupId + "", openId);
            System.out.println(ress);
            if(ress > 0){
                return ress > 0;
            } else {
                int res = groupMapper.addGroup(groupId, openId, isA);
                return res > 0;
            }
        } else {
            return false;
        }
    }
}
